/*
 * Copyright (C) 2008 GeoScheduler team, as stated on <http://www.geoscheduler.org/authors>.
 * 
 * This file is part of GeoScheduler.
 * 
 * GeoScheduler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GeoScheduler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoScheduler. If not, see <http://www.gnu.org/licenses/>.
 */
package org.geoscheduler.android.sync;

import org.geoscheduler.android.activities.EditEvent;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;

/**
 * Tool for registering and unregistering sync timer
 * 
 * @author paja
 */
public class SyncEventsHelper {

	protected final static long MINIMUM_TIME_BETWEEN_SYNC = 30000; // 30 seconds 
	protected final static long MINIMUM_DISTANCECHANGE_FOR_SYNC = 100; // 100 meters
	
	protected static LocationListener syncEvLL;
	
	public static void registerIntent(Context context) {

		syncEvLL = new SyncEventsHelper.SyncEventLocListener(context);

		// Find location service
        LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

		LocationProvider provider = lm.getProvider(EditEvent.FIXED_LOCATION_PROVIDER);
		
		// Register intent to location manager
		lm.requestLocationUpdates(provider.getName(),
				MINIMUM_TIME_BETWEEN_SYNC,
				MINIMUM_DISTANCECHANGE_FOR_SYNC, 
				syncEvLL);

        // Tell the user about what happened
//        Toast.makeText(context, R.string.al_syncevent_registered, Toast.LENGTH_LONG).show();
	}

	public static void unregisterIntent(Context context) {
		// Find location service
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

		// Unregister intent to location manager
		locationManager.removeUpdates(syncEvLL);

        // Tell the user about what happened
//        Toast.makeText(context, R.string.al_syncevent_unregistered, Toast.LENGTH_LONG).show();
	}
	
	public static class SyncEventLocListener implements LocationListener {
		Context ctx;
		
		public SyncEventLocListener(Context ctx) {
			super();
			this.ctx = ctx;
		}

		/**
		 * @see android.location.LocationListener#onLocationChanged(android.location.Location)
		 */
		@Override
		public void onLocationChanged(Location location) {
			ctx.startService(new Intent(ctx, SyncEventsService.class));
			
		}
		
		// FIXME do we have to handle these?

		/**
		 * @see android.location.LocationListener#onProviderDisabled(java.lang.String)
		 */
		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			
		}

		/**
		 * @see android.location.LocationListener#onProviderEnabled(java.lang.String)
		 */
		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			
		}

		/**
		 * @see android.location.LocationListener#onStatusChanged(java.lang.String, int, android.os.Bundle)
		 */
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		}
		
	}
}
